package com.songlanyun.modules.offlineorder.controller;

import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.R;
import com.songlanyun.modules.account.controller.common.AbstractApiController;
import com.songlanyun.modules.offlineorder.dto.OfflineOrderBuildDto;
import com.songlanyun.modules.offlineorder.dto.OfflineOrderBuildVo;
import com.songlanyun.modules.offlineorder.dto.OfflineOrderCountVo;
import com.songlanyun.modules.offlineorder.entity.OfflineOrder;
import com.songlanyun.modules.offlineorder.service.OfflineOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;


/**
 * 线下商家上报订单
 *
 * @author lubo
 * @email sunlightcs@gmail.com
 * @date 2023-02-11 16:57:11
 */
@Slf4j
@RestController
@RequestMapping("api/offlineorder")
public class OfflineOrderApiController  extends AbstractApiController {
    @Autowired
    private OfflineOrderService offlineOrderService;

    /**
     * 商家上报的订单列表
     */
    @RequestMapping("/page")
    //@RequiresPermissions("offlineorder:offlineorder:list")
    public R page(@RequestParam Map<String, Object> params){
        params.put("shop_id",getShopByUserId(getUserId(true),true, true));
        PageUtils page = offlineOrderService.queryPage(params);
        return R.ok().put("page", page);
    }

    /**
     * 我的线下订单
     */
    @RequestMapping("/my_page")
    //@RequiresPermissions("offlineorder:offlineorder:list")
    public R myPage(@RequestParam Map<String, Object> params){
        params.put("user_id",getUserId(true));
        PageUtils page = offlineOrderService.queryPage(params);
        return R.ok().put("page", page);
    }
    /**
     * 商家上报的订单详情
     */
    @GetMapping("/info/{id}")
    //@RequiresPermissions("offlineorder:offlineorder:info")
    public R info(@PathVariable("id") Long id){
		OfflineOrder offlineOrder = offlineOrderService.getById(id);
		offlineOrder.loadShop();
		offlineOrder.loadComments();
        return R.ok().put("data", offlineOrder);
    }

    /**
     * 线下商家的线下订单统计详情
     */
    @GetMapping("/count")
    //@RequiresPermissions("offlineorder:offlineorder:info")
    public R count(@RequestParam Map<String, Object> params){
        params.put("shop_id",getShopByUserId(getUserId(true),true, true));
        OfflineOrderCountVo countVo = offlineOrderService.dateCount(params);
        return R.ok().put("data", countVo);
    }

    /**
     * 会员的的线下订单统计详情
     */
    @GetMapping("/mycount")
    //@RequiresPermissions("offlineorder:offlineorder:info")
    public R mycount(@RequestParam Map<String, Object> params){
        params.put("user_id",getUserId(true));
        OfflineOrderCountVo countVo = offlineOrderService.dateCount(params);
        return R.ok().put("data", countVo);
    }


    /**
     * 构建订单
     */
    @PostMapping("/build")
    //@RequiresPermissions("offlineorder:offlineorder:save")
    public R build(@RequestBody OfflineOrderBuildDto offlineOrderBuildDto){
        offlineOrderBuildDto.setShopId(getShopByUserId(getUserId(true),true, true));
        OfflineOrderBuildVo vo = offlineOrderService.build(offlineOrderBuildDto);
        return  R.ok().put("data", vo);
    }

    /**
     * 上报订单
     */
    @PostMapping("/create")
    //@RequiresPermissions("offlineorder:offlineorder:save")
    public R save(@RequestBody OfflineOrderBuildDto offlineOrderBuildDto){
        offlineOrderBuildDto.setShopId(getShopByUserId(getUserId(true),true, true));
		offlineOrderService.createOrder(offlineOrderBuildDto);
        return R.ok();
    }






}
